package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.BidDetailsActivity;
import com.kkd.kkdapp.activity.ExperienceParticularsActivity;
import com.kkd.kkdapp.activity.RegularParticularsActivity;
import com.kkd.kkdapp.activity.RepaymentDetailActivity;
import com.kkd.kkdapp.http.entity.TenderDetailMapList;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.RoundProgressBar;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyRegularAdapter extends BaseAdapter {

	private final int POSITION_ONE = 0;

	private List<TenderDetailMapList> mList;
	private Context context;
	private PopupWindow mDialog;

	public MyRegularAdapter(Context context, List<TenderDetailMapList> mList) {
		this.mList = mList;
		this.context = context;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		OtherViewHolder mOtherViewHolder;
		if (convertView != null
				&& (convertView.getTag() instanceof OtherViewHolder)) {
			mOtherViewHolder = (OtherViewHolder) convertView.getTag();
		} else {
			mOtherViewHolder = new OtherViewHolder();
			convertView = View.inflate(context, R.layout.myregular_other_item,
					null);
			mOtherViewHolder.refund_name = (TextView) convertView
					.findViewById(R.id.refund_name);
			mOtherViewHolder.interest_tv = (TextView) convertView
					.findViewById(R.id.interest_tv);
			mOtherViewHolder.deadline_company_tv = (TextView) convertView
					.findViewById(R.id.deadline_company_tv);
			mOtherViewHolder.bid = (TextView) convertView
					.findViewById(R.id.bid);
			mOtherViewHolder.title = (RelativeLayout) convertView
					.findViewById(R.id.title);
			mOtherViewHolder.centre = (LinearLayout) convertView
					.findViewById(R.id.centre);
			mOtherViewHolder.progressBar1 = (RoundProgressBar) convertView
					.findViewById(R.id.progressBar1);
			mOtherViewHolder.term = (TextView) convertView
					.findViewById(R.id.term);
			mOtherViewHolder.company_tv = (TextView) convertView
					.findViewById(R.id.company_tv);
			mOtherViewHolder.top = (RelativeLayout) convertView
					.findViewById(R.id.top);
			mOtherViewHolder.company_tv1 = (TextView) convertView
					.findViewById(R.id.company_tv1);
			convertView.setTag(mOtherViewHolder);
		}
		// 标的名称
		mOtherViewHolder.refund_name.setText(mList.get(position).getBidTitle());
		// 投标金额
		mOtherViewHolder.interest_tv
				.setText(mList.get(position).getPoiAmount());
		// 投标期限
		mOtherViewHolder.term.setText("" + mList.get(position).getBidPeriod());
		if ("天".equals(mList.get(position).getBidPeriodName2())) {
			mOtherViewHolder.company_tv.setText(mList.get(position)
					.getBidPeriodName2());
		} else if ("月".equals(mList.get(position).getBidPeriodName2())) {
			mOtherViewHolder.company_tv.setText("个"
					+ mList.get(position).getBidPeriodName2());
		}
		if (mList.get(position).getBidPeriodName().indexOf("+") == -1) {
			mOtherViewHolder.company_tv1.setText("");
		} else {
			String[] bidperiodName = mList.get(position).getBidPeriodName()
					.split("\\+");
			mOtherViewHolder.company_tv1.setText("+" + bidperiodName[1]);
		}
		// 投标日期
		mOtherViewHolder.deadline_company_tv.setText("投标时间 "
				+ mList.get(position).getPoiDate());
		// 进度条
		String bidAmount = mList.get(position).getBidAmount();
		String bidFreeAmount = mList.get(position).getBidFreeAmount();
		double interest = (Double.parseDouble(bidAmount) - Double
				.parseDouble(bidFreeAmount)) / Double.parseDouble(bidAmount);
		int interest1 = (int) (interest * 100);
		mOtherViewHolder.progressBar1.setTextSize(SystemUtils.dip2px(context,
				13));
		mOtherViewHolder.progressBar1.setRoundWidth(SystemUtils.dip2px(context,
				3));
		// 进度色值
		mOtherViewHolder.progressBar1.setCricleProgressColor(Color
				.parseColor("#ff841f"));
		// 圆环色值
		mOtherViewHolder.progressBar1.setCricleColor(Color
				.parseColor("#eeeeee"));
		// 进度字体色值
		mOtherViewHolder.progressBar1.setTextColor(Color.parseColor("#ff841f"));
		mOtherViewHolder.progressBar1.setPropress(mList.get(position)
				.getBidProgress() + "%");
		// if (Double.parseDouble(mList.get(position).getBidProgress()) < 1) {
		// mOtherViewHolder.progressBar1.setProgress(1);
		// } else if (Double.parseDouble(mList.get(position).getBidProgress()) >
		// 1) {
		//
		// }
		mOtherViewHolder.progressBar1.setProgress(interest1);
		// 标的状态
		String bidStatusCode = mList.get(position).getBidStatusCode();
		mOtherViewHolder.bid.setVisibility(View.GONE);
		mOtherViewHolder.bid
				.setBackgroundResource(R.drawable.tender_img_returning);
		mOtherViewHolder.progressBar1.setVisibility(View.GONE);
		if ("5002".equals(bidStatusCode)) {
			mOtherViewHolder.progressBar1.setVisibility(View.VISIBLE);
		} else if ("5003".equals(bidStatusCode)) {
			mOtherViewHolder.bid.setVisibility(View.VISIBLE);
			mOtherViewHolder.bid
					.setBackgroundResource(R.drawable.tender_img_man);
		} else if ("5004".equals(bidStatusCode)) {
			mOtherViewHolder.bid.setVisibility(View.VISIBLE);
			mOtherViewHolder.bid
					.setBackgroundResource(R.drawable.tender_img_returning);
		}

		// 投标详情
		mOtherViewHolder.top.setOnClickListener(new OnClickListener() {

			String poiId = mList.get(position).getPoiId();

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, BidDetailsActivity.class);
				intent.putExtra("poiId", poiId);
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	class OtherViewHolder {
		TextView refund_name;
		TextView interest_tv;
		TextView deadline_company_tv;
		TextView bid;
		TextView term;
		RelativeLayout title, top;
		LinearLayout centre;
		RoundProgressBar progressBar1;
		TextView company_tv, company_tv1;
	}

}
